﻿<?php
/* Description: Retrieves a list of the most recent comments. */
if (function_exists('ihao_recent_comments')) { 
}else{
		
	function ihao_recent_comments($no_comments = 10, $comment_lenth = 86, $before = '<li class="off" onmouseover=this.className="on"; onmouseout=this.className="off";>', $after = '</li>', $show_pass_post = false, $comment_style = 0) {
	    global $wpdb;
	    $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
		if(!$show_pass_post) $request .= "AND post_password ='' ";
		$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";
		$comments = $wpdb->get_results($request);
	    $output = '';
		if ($comments) {
			foreach ($comments as $comment) {
				$comment_author = stripslashes($comment->comment_author);
				if ($comment_author == "")
					$comment_author = "anonymous"; 
				$comment_content = strip_tags($comment->comment_content);
				$comment_content = stripslashes($comment_content);
				$words=split(" ",$comment_content); 
				$comment_excerpt = substr($comment_content,0,$comment_lenth);
				$comment_excerpt = ihao_utf8_trim($comment_excerpt);
				$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
	
				if ($comment_style == 1) {
					$post_title = stripslashes($comment->post_title);
					
					$url = $comment->comment_author_url;
	
					if (empty($url))
						$output .= $before . $comment_author . ' on ' . $post_title . '.' . $after;
					else
						$output .= $before . "<a href='$url' rel='external'>$comment_author</a>" . ' on ' . $post_title . '.' . $after;
				}
				else {
					$output .= $before . '<a href="' . $permalink . '" title="View the entire comment by ' . $comment_author.'">' . $comment_author . '</a>:  ' . $comment_excerpt.'' . $after;
					
				}
			}
			$output = convert_smilies($output);
		} else {
			$output .= $before . "None found" . $after;
		}
	    echo $output;
	}
}

// A trim function to remove the last character of a utf-8 string
function ihao_utf8_trim($str) {

	$len = strlen($str);

	for ($i=strlen($str)-1; $i>=0; $i-=1){
		$hex .= ' '.ord($str[$i]);
		$ch = ord($str[$i]);
        if (($ch & 128)==0) return(substr($str,0,$i));
		if (($ch & 192)==192) return(substr($str,0,$i));
	}
	return($str.$hex);
}

//get random posts
function ihao_random_posts ($limit = 5, $length = 400, $before = '<li>', $after = '</li>', $show_pass_post = false, $show_excerpt_in_title = true) {
    global $wpdb;
    $sql = "SELECT ID, post_title, post_date, post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ";
	if(!$show_pass_post) $sql .= "AND post_password ='' ";
	$sql .= "ORDER BY RAND() LIMIT $limit";
    $posts = $wpdb->get_results($sql);
	$output = '';
    foreach ($posts as $post) {
       $post_title = stripslashes($post->post_title);
	$post_date = mysql2date('m/d/Y', $post->post_date);
       $permalink = get_permalink($post->ID);
	$post_content = strip_tags($post->post_content); 
	$post_content = stripslashes($post_content); 
	$post_strip = substr($post_content,0,$length);
       $post_strip = ihao_utf8_trim($post_strip);
	$post_strip = str_replace('"', '', $post_strip);
	$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="';
	if($show_excerpt_in_title) {
		$output .= $post_strip . '...  ';
      	   } else  {
		$output .= 'Permanent Link: ' . str_replace('"', '', $post_title) . '...   ';
	}
	$output .= $post_date . '">' . $post_title . '</a>';
	if(!$show_excerpt_in_title) {
		$output .= ': ' . $post_strip . '...  ';
      	   }
	$output .= $after;
	}
	echo $output;
}

function ihao_get_same_category_post($ID, $limit, $order="DESC"){
	$before="<li>";
	$end="</li>";
	global $wpdb, $tableposts;
	$sql = "SELECT category_id FROM $wpdb->post2cat WHERE post_id = $ID";
	$categoryID = $wpdb->get_var($sql);
	if($order=="rand")
		$posts = get_posts('numberposts='.$limit.'&orderby=rand()&category='.$categoryID);
	else
		$posts = get_posts('numberposts='.$limit.'&orderby=post_date&order='.$order.'&category='.$categoryID);
	$output = '';
    foreach ($posts as $post) {
		$post_title = stripslashes($post->post_title);
		$post_date = mysql2date('j.m.Y', $post->post_date);
		$permalink = get_permalink($post->ID);
		$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="';
		$output .= 'Permanent Link: ' . str_replace('"', '', $post_title) . '...   ';
		$output .= $post_date . '">' . $post_title . '</a>'.$end;
	}
	echo $output;
}

// Get Comments' Members Stats
// Treshhold = Number Of Posts User Must Have Before It Will Display His Name Out
// 5 = Default Treshhold; -1 = Disable Treshhold
function ihao_get_commentmembersstats($threshhold = 5) {
	global $wpdb, $tablecomments;
	$comments = $wpdb->get_results("SELECT comment_author, comment_author_url, COUNT(comment_ID) AS 'comment_total' FROM $tablecomments WHERE comment_approved = '1' AND (comment_author != 'ihao') AND (comment_author != '')GROUP BY comment_author ORDER BY comment_total DESC");
	$no = 1;

    foreach ($comments as $comment) {
			$comment_author = htmlspecialchars(stripslashes($comment->comment_author));
			$comment_author_url =stripslashes($comment->comment_author_url);
			$comment_total = (int) $comment->comment_total;
				if ($comment_author_url) {
					$comment_author_link = "<li><a href='$comment_author_url' target='_blank'>$comment_author</a></li>";
				} else {
					$comment_author_link = "<li><a href='mailto:$comment_author_email'>$comment_author</a></li>";
				}
			echo "<li><a href=\"$comment_author_url\" target=\"_blank\">$comment_author</a> ($comment_total) </li> ";
			$no++;

			// If Total Comments Is Below Threshold
			if($comment_total <= $threshhold && $threshhold != -1) {
				return;
			}

    }
}
?>